<?php declare(strict_types=1);

namespace App\Console\Commands\SyncOrderStatus;


use App\Api\Controllers\Merchant\OrderController;
use App\Models\Bi\BillLog;
use App\Models\Order;
use App\Services\PrintSqlService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use Tymon\JWTAuth\Facades\JWTAuth;

class QueryOrderStatusCommand extends Command
{
    protected $signature = 'query:order';
    protected $description = 'query:order';

    public function __construct()
    {
        parent::__construct();
    }

    //同步订单状态
    public function handle()
    {
        //运行定时任务
        $this->syncOrderStatus();
    }

    public function syncOrderStatus()
    {
        $bilogs = BillLog::query()->where("store_pid", "20247261930053516")
            ->where("day", "2025-06-30")
            ->where("status", 1)
            ->get();

        foreach ($bilogs as $bilog) {
            dump($bilog->out_trade_no);
            $order = Order::query()->where("out_trade_no", $bilog->out_trade_no)->first();
            $this->updateOrder($order->store_id, $order->out_trade_no);
        }
    }

    public function updateOrder(string $store_id, string $out_trade_no): void
    {
        try {
            $a = array('a', '_', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
            $b = str_ireplace($a, "", $out_trade_no);
            $day = substr($b, 0, 8);
            $table = 'orders_' . $day;

            if (Schema::hasTable($table)) {
                $order = Order::query()->from($table)->where('out_trade_no', $out_trade_no)
                    ->where('store_id', $store_id)
                    ->first();
            } else {
                $order = Order::where('out_trade_no', $out_trade_no)
                    ->where('store_id', $store_id)
                    ->first();
            }
            if (!$order) {
                return;
            }
            if ($order->pay_status == 6) {
                return;
            }
            $data = [
                'out_trade_no' => $order->out_trade_no,
                'store_id' => $order->store_id,
                'ways_type' => $order->ways_type,
                'config_id' => $order->config_id,
                'table' => $table,
            ];
            $obj = new OrderController();
            $return = $obj->order_foreach_public($data);
            if (!is_string($return) || empty($return = json_decode($return, true))) {
                Log::channel("searchOrder")->error("json_decodeStatus" . date("Y-m-d H:i:s", time()), [
                    "out_trade_no" => $order->out_trade_no,
                    "return" => $return
                ]);
                return;
            }
            //处理支付失败订单 1
            if ($return['pay_status'] == '2' ) {
                Log::channel("searchOrder")->info("search" . date("Y-m-d H:i:s", time()), [
                    "out_trade_no" => $order->out_trade_no,
                    "return" => $return
                ]);
            }
        } catch (\Throwable $exception) {
            Log::channel("searchOrder")->error("exceptionStatus" . date("Y-m-d H:i:s", time()), [
                "out_trade_no" => $order->out_trade_no,
                "msg" => $exception->getMessage(),
                "line" => $exception->getLine(),
                "file" => $exception->getFile()
            ]);
        }
    }

}
